/**
 * Project Name:mauto-web
 * File Name:ApiCommentController.java
 * Package Name:com.inftech.mauto.core.web.front
 * Date:2016年10月17日下午9:50:11
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.web.front;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.constant.SessionConstEnum;
import com.inftech.mauto.core.entity.Comment;
import com.inftech.mauto.core.service.CommentService;
import com.mingsoft.weixin.entity.WeixinPeopleEntity;

/**
 * ClassName:ApiCommentController <br/>
 * Function: 评价接口. <br/>
 * Date: 2016年10月17日 下午9:50:11 <br/>
 * 
 * @author jeno
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/api/comment")
public class ApiCommentController extends BaseController {

	/**
	 * 注入评价业务层
	 */
	@Autowired
	protected CommentService commentService;

	/**
	 * 
	 * save:新增评价. <br/>
	 * 
	 * @author puyf
	 * @param comment
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String save(@RequestBody Comment comment, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			commentService.saveComment(comment, people);
			LOG.info("新增评价成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("新增评价失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * list:获取评价列表. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/list", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String list(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			// 获取我的评价列表
			List<Map<String, Object>> carDoctorList = commentService.queryCommentListFront(people);
			LOG.info("获取我的评价列表成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), carDoctorList);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("获取我的评价列表失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}
}
